const rewire = require('rewire')
const {makeFetcher, wrapProxy, makeAxios} = rewire('../common/fetch.cjs')
const proxy = rewire('../common/proxy.cjs')
const R = require('ramda')
const log4js = require('log4js');
const {getLogger} = rewire('../common/logger.cjs')

async function main1() {
  let fetch = wrapProxy(makeFetcher('get'), 1)
  const rs = await fetch('https://www.baidu.com')
  console.log(rs)
}

async function main2() {
  // const dd = await proxy.getProxyList(3)
  // console.log(dd)
  
  // const ee = await proxy.getValidTime(dd)
  // console.log(ee)

  // const ipList = [
  //   '183.128.43.234:23215',
  //   '27.154.220.199:19315',
  //   '117.27.116.240:16619'
  // ]

  // const ff = await proxy.getValidTime(ipList)
  // console.log(ff)



  // console.log(1)
  // await fetch('https://www.baidu.com')
  // console.log(1)
  // await fetch('https://www.baidu.com')
  // console.log(1)
  // await fetch('https://www.baidu.com')

  // let fetch = makeFetcher('get')
  // try {
  //   await fetch('https://www.baidu1.com')
  // } catch(e) {
  //   console.log(e.message)
  // }
  
  // const config = await proxy.getDouyaProxyConfig()
  // console.log(config)
  // const fn = makeAxios({method: 'GET', ...config})
  // try {
  //   const rs = await fn("https://h.douyaruanjian.com/ucenter/")
  //   console.log(rs)
  // } catch(e) {
  //   console.log(e)
  // }
  
}

async function main3() {
  const itemList = R.range(0, 10)
  const iter = async n => {
    const rs = await proxy.getDouyaProxyConfig(10)
    return {n, rs}
  }
  const configs = await Promise.all(itemList.map(iter))
  console.log(configs[0].n)
  console.log(configs[0].rs)
  //const fn = makeAxios({method: 'GET', ...configs[6]})
  //const data = await fn("https://h.douyaruanjian.com/ucenter/")
  //console.log(data)
}


async function testLogger() {
  
  const logger = log4js.getLogger('testProxy');
  logger.level = "all"

  logger.trace('this is trace');
  logger.debug('this is debug');
  logger.info('this is info');
  logger.warn('this is warn');
  logger.error('this is error');
  logger.fatal('this is fatal');
  logger.mark('this is mark');
}

async function testLogger2() {
  log4js.configure({
    appenders: { 
      dateFile: { 
        type: "dateFile",
        encoding: 'utf-8',
        pattern: "yyyy-MM-dd",
        filename: "./logs/make-money.log",
        keepFileExt: true,
        alwaysIncludePattern: true
      },
      stdout: { type: 'stdout' }
    },
    categories: { default: { appenders: ["dateFile", "stdout"], level: "debug" } },
  });

  const logger = log4js.getLogger('testProxy');
  logger.debug('this is debug');
  logger.info('this is info');
  logger.warn('this is warn');
  logger.error('this is error');
}

function testLogger3() {
  const logger = getLogger("xxx")
  logger.warn("aaaaaa")
}


testLogger3()